package com.west_traveler.recyclerview_adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.west_traveler.entity.entity_anchor.Item_list_Anchor;
import com.west_traveler.entity.entity_anchor.entiti_list_anchor;
import com.west_traveler.ximalayafm.R;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by Administrator on 12/3 0003.
 */

public class AnchorFraRecyclerViewAdapte extends RecyclerView.Adapter<AnchorFraRecyclerViewAdapte.ViewHolder>{
    private Context context;
    private List<Item_list_Anchor> anchorDatas;

    public AnchorFraRecyclerViewAdapte(Context context) {
        this.context = context;
        anchorDatas = new LinkedList<>();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_recycler_anchor, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    public void addAll(Collection<? extends Item_list_Anchor> collection) {
        int size = anchorDatas.size();
        anchorDatas.addAll(collection);
        notifyItemRangeInserted(size, collection.size());
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Item_list_Anchor item_list_anchor = anchorDatas.get(position);
        List<entiti_list_anchor> itemDatas = item_list_anchor.getItemDatas();
        holder.title.setText(item_list_anchor.getTitle());

       if (itemDatas!=null){
           holder.nickname1.setText(itemDatas.get(0).getNickname());
           Picasso.with(context).load(itemDatas.get(0).getSmallLogo()).into(holder.photo1);
           photoListener(holder.photo1,0,itemDatas);

           holder.nickname2.setText(itemDatas.get(1).getNickname());
           Picasso.with(context).load(itemDatas.get(1).getSmallLogo()).into(holder.photo2);
           photoListener(holder.photo2,1,itemDatas);

           holder.nickname3.setText(itemDatas.get(2).getNickname());
           Picasso.with(context).load(itemDatas.get(2).getSmallLogo()).into(holder.photo3);
           photoListener(holder.photo3,2,itemDatas);
       }



    }

    public void photoListener(ImageView view, final int i, final List<entiti_list_anchor> itemDatas){
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String nickname = itemDatas.get(i).getNickname();
                Toast.makeText(context, "点击了图片的作者:"+nickname,Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public int getItemCount() {
        return anchorDatas.size();
    }





















    public class ViewHolder extends RecyclerView.ViewHolder{


        private final ImageView photo1;
        private final TextView nickname1;
        private final ImageView focus1;

        private final ImageView photo2;
        private final TextView nickname2;
        private final ImageView focus2;

        private final ImageView photo3;
        private final TextView nickname3;
        private final ImageView focus3;
        private final TextView title;


        public ViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.c0_anchor).findViewById(R.id.tv_find_fra_sound_category_id);

            photo1 = (ImageView) itemView.findViewById(R.id.c1_anchor).findViewById(R.id.iv_photo_anchor);
            nickname1 = (TextView) itemView.findViewById(R.id.c1_anchor).findViewById(R.id.tv_nickname_anchor);
            focus1 = (ImageView) itemView.findViewById(R.id.c1_anchor).findViewById(R.id.iv_focus_anchor);

            photo2 = (ImageView) itemView.findViewById(R.id.c2_anchor).findViewById(R.id.iv_photo_anchor);
            nickname2 = (TextView) itemView.findViewById(R.id.c2_anchor).findViewById(R.id.tv_nickname_anchor);
            focus2 = (ImageView) itemView.findViewById(R.id.c2_anchor).findViewById(R.id.iv_focus_anchor);

            photo3 = (ImageView) itemView.findViewById(R.id.c3_anchor).findViewById(R.id.iv_photo_anchor);
            nickname3 = (TextView) itemView.findViewById(R.id.c3_anchor).findViewById(R.id.tv_nickname_anchor);
            focus3 = (ImageView) itemView.findViewById(R.id.c3_anchor).findViewById(R.id.iv_focus_anchor);

        }
    }
}
